#include <dspSimulator/targetMachine/dspMachine.h>
#include <dspSimulator/targetMachine/dspRegisterInfo.h>
#include <dspSimulator/register/generalRegister.h>
#include <dspSimulator/register/controlRegister.h>

DspMachine::DspMachine() {
    for (auto &name: RegAName) {
        std::shared_ptr<Register<u32>> reg = std::make_shared<GPR>(name);
        m_regs->addRegister32(reg);
    }
    for (auto &name: RegBName) {
        std::shared_ptr<Register<u32>> reg = std::make_shared<GPR>(name);
        m_regs->addRegister32(reg);
    }

    // 设置所有控制寄存器的掩码
    for (u32 i = 0; i <static_cast<u32>(CRegKind::NUM_OF_CONTROL_REG); i++) {
        u32 mask = 0;
        CRegKind kind = static_cast<CRegKind>(i);
        switch (kind) {
            case CRegKind::AMR:
                mask = 0X03FFFFFF;
                break;
            case CRegKind::GFPGFR:
                mask = 0X070000FF;
                break;
            case CRegKind::ICR:
            case CRegKind::ISR:
                mask = 0X0000FFF0;
                break;
            case CRegKind::IER:
            case CRegKind::IFR:
                mask = 0X0000FFF3;
                break;
            case CRegKind::IRP:
            case CRegKind::ISTP:
            case CRegKind::NRP:
            case CRegKind::PCE1:
            case CRegKind::GPLYA:
            case CRegKind::GPLYB:
            case CRegKind::ILC:
            case CRegKind::TSCL:
            case CRegKind::TSCH:
            case CRegKind::CSR:
            case CRegKind::REP:
            case CRegKind::RILC:
                mask = 0XFFFFFFFF;
                break;
            case CRegKind::DNUM:
                mask = 0X000000FF;
                break;
            case CRegKind::EFR:
                mask = 0xC0000003;
                break;
            case CRegKind::IERR:
                mask = 0x000001FF;
                break;
            case CRegKind::ITSR:
                mask = 0x0000C7CF;
                break;
            case CRegKind::NTSR:
                mask = 0x0001C7CF;
                break;
            case CRegKind::SSR:
                mask = 0x0000003F;
                break;
            case CRegKind::TSR:
                mask = 0x0000C7CF;
                break;
            case CRegKind::FADCR:
            case CRegKind::FAUCR:
            case CRegKind::FMCR:
                mask = 0x07FF07FF;
                break;
            default:
                mask = 0XFFFFFFFF;
        }
        std::shared_ptr<Register<u32>> reg = std::make_shared<CReg>(CRegName[i], mask);
        m_regs->addRegister32(reg);
    }

    // 添加指令指针寄存器pc
    std::shared_ptr<Register<u32>> pc = std::make_shared<CReg>("pc", 0xffffffff);
    m_regs->addRegister32(pc);
}